import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:osc/common/MyTextStyle.dart';
import 'package:osc/res/colors.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/widgets/button/pd_button.dart';
import 'package:osc/ui/widgets/common_scaffold.dart';
import 'package:osc/ui/widgets/small_widgets.dart';
import 'package:osc/utils/pd_util.dart';
import 'package:osc/utils/string_util.dart';

/// 投递文章
class DeliverPost extends StatefulWidget {
  @override
  _DeliverPostState createState() => _DeliverPostState();
}

class _DeliverPostState extends State<DeliverPost> {

  TextEditingController urlEditingController;
  TextEditingController titleEditingController;
  FocusNode focusNodeUrl;
  FocusNode focusNodeTitle;

  @override
  void initState() {
    super.initState();
    urlEditingController = TextEditingController();
    titleEditingController = TextEditingController();
    focusNodeUrl = FocusNode();
    focusNodeTitle = FocusNode();
  }

  @override
  void dispose() {
    super.dispose();
    titleEditingController.dispose();
    urlEditingController.dispose();
    focusNodeTitle.dispose();
    focusNodeUrl.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CommonScaffold(
      bgColor: Colours.bg_page,
        title: "投递文章",
        body: ListView(
          padding: EdgeInsets.symmetric(vertical: Dimens.gap_activity_vertical),
          children: <Widget>[
            buildInput("原始链接", "http://***", focusNodeUrl, urlEditingController, keyboardType: TextInputType.url),
            SmallWidgets.buildLine(),
            buildInput("文章标题", "分享标题", focusNodeTitle, titleEditingController, keyboardType: TextInputType.text),
            buildTips(),
            Divider(color: Colors.transparent,),
            PdButton(
              bg: Colors.green,
              data: "提交",
              width: ScreenUtil.getScreenW(context),
              margin: EdgeInsets.symmetric(horizontal: Dimens.gap_activity_horizontal),
              padding: EdgeInsets.symmetric(vertical: Dimens.gap_view_bigger),
              onTap: (){
                checkDeliver();
              },
            ),
          ],
        ),
    );
  }

  buildInput(String data, String hint, FocusNode focusNode, TextEditingController controller, {TextInputType keyboardType}){
    return Container(
      padding: EdgeInsets.symmetric(horizontal: Dimens.gap_activity_horizontal),
      color: Colors.white,
      child: Row(
        children: <Widget>[
          Padding(
              padding: EdgeInsets.only(right: Dimens.gap_view_normal),
            child: Text(data, style: MyTextStyle.USER_LIST_TITLE,),
          ),
          buildTextField(hint, focusNode, controller, keyboardType: keyboardType),
        ],
      ),
    );
  }

  buildTextField(String hint, FocusNode focusNode, TextEditingController controller, {TextInputType keyboardType}){
    return Expanded(
        child: TextField(
          style: MyTextStyle.TEXT_FIELD_COMMENT,
          maxLines: 1,
          focusNode: focusNode,
          controller: controller,
          keyboardType: keyboardType,
          decoration: InputDecoration(
            hintText: hint,
            hintStyle: MyTextStyle.HINT,
            border: InputBorder.none,
            focusedBorder: InputBorder.none,
            errorBorder: InputBorder.none,
            focusedErrorBorder: InputBorder.none,
            disabledBorder: InputBorder.none,
            enabledBorder: InputBorder.none,
          ),
        ),
    );
  }

  buildTips(){
    return Padding(
      padding: EdgeInsets.all(Dimens.gap_activity_horizontal),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          buildTipsChild("1、将要分享的文件的 URL 粘贴到“原始链接”输入框内"),
          buildTipsChild("2、APP 将尝试获取文章的标题，您也可以手动填写标题"),
          buildTipsChild("3、提交的文章需要审核，请保证文章信息真实有效，我们会在一个工作日内审核完成"),
          Divider(color: Colors.transparent,),
          buildTipsChild("进入“我的”>“设置”关闭“自动检测粘贴板中的URL”开头可以取消分享提醒。"),
        ],
      ),
    );
  }

  buildTipsChild(String data){
    return Text(data, style: MyTextStyle.LIST_BODY,);
  }

  checkDeliver(){
    if(StringUtil.isEmpty(urlEditingController.text)){
      showTips("请输入原始链接");
    } else if(!RegexUtil.isURL(urlEditingController.text)){
      showTips("请输入正确链接地址");
    }
  }

  showTips(String msg){
    PdUtil.showToast(msg: msg);
  }
}
